package com.study.mongo;

import lombok.Data;

/**
 * @author ：ihao ∶）
 * @date ：Created in 2021-01-14 11:16
 */
@Data
//@Builder
public class MongoSelectDTO {

	private Integer pageNo;
	private Integer pageSize;
	private Integer limit;
	private String sortName;
	private Boolean desc;

	public MongoSelectDTO(Integer pageNo, Integer pageSize, String sortName, Boolean desc, Integer limit) {
		this.setPageNo(pageNo);
		this.setPageSize(pageSize);
		this.setSortName(sortName);
		this.setDesc(desc);
		this.setLimit(limit);
	}

	public MongoSelectDTO(Integer pageNo, Integer pageSize, String sortName, Boolean desc) {
		this.setPageNo(pageNo);
		this.setPageSize(pageSize);
		this.setSortName(sortName);
		this.setDesc(desc);
		this.setLimit(1);
	}

	public MongoSelectDTO(Integer pageNo, Integer pageSize, Integer limit) {
		this.setPageNo(pageNo);
		this.setPageSize(pageSize);
		this.setLimit(limit);
	}

	public MongoSelectDTO(Integer pageNo, Integer pageSize) {
		this.setPageNo(pageNo);
		this.setPageSize(pageSize);
	}

	public int getStart() {
		return (pageNo - 1) * pageSize;
	}

	public void setPageNo(Integer pageNo) {
		if (pageNo == null || pageNo < 1) {
			pageNo = 1;
		}
		this.pageNo = pageNo;
	}

	public void setPageSize(Integer pageSize) {
		if (pageSize == null || pageSize < 1) {
			pageSize = 20;
		}
		this.pageSize = pageSize;
	}

	public boolean isDesc() {
		return this.sortName != null && this.desc != null ? this.desc : false;
	}

}
